package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.core.common.constant.factory.PageFactory;
import cn.stylefeng.guns.core.common.page.PageInfoBT;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.modular.system.dto.DeviceRecordQuery;
import cn.stylefeng.guns.modular.system.model.DeviceRecord;
import cn.stylefeng.guns.modular.system.model.OperationLog;
import cn.stylefeng.guns.modular.system.service.IDeviceRecordService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 设备统计表控制器
 *
 * @author fengshuonan
 * @Date 2018-11-25 01:42:44
 */
@Controller
@RequestMapping("/deviceRecord")
public class DeviceRecordController extends BaseController {
    private Logger log=LoggerFactory.getLogger(this.getClass().getName());
    private String PREFIX = "/system/deviceRecord/";

    @Autowired
    private IDeviceRecordService deviceRecordService;

    /**
     * 跳转到设备统计表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "deviceRecord.html";
    }

    /**
     * 跳转到添加设备统计表
     */
    @RequestMapping("/deviceRecord_add")
    public String deviceRecordAdd() {
        return PREFIX + "deviceRecord_add.html";
    }

    /**
     * 跳转到修改设备统计表
     */
    @RequestMapping("/deviceRecord_update/{deviceRecordId}")
    public String deviceRecordUpdate(@PathVariable String deviceRecordId, Model model) {
        DeviceRecord deviceRecord = deviceRecordService.selectById(deviceRecordId).get();
        model.addAttribute("item",deviceRecord);
        LogObjectHolder.me().set(deviceRecord);
        return PREFIX + "deviceRecord_edit.html";
    }

    /**
     * 获取设备统计表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(DeviceRecordQuery deviceRecordQuery) {
        log.info("Query Info is {}",JSON.toJSONString(deviceRecordQuery));
        Page<DeviceRecord> page = new PageFactory<DeviceRecord>().defaultPage();
         org.springframework.data.domain.Page<DeviceRecord> deviceRecordPage= deviceRecordService.getDeviceRecords(page,deviceRecordQuery);
        List<DeviceRecord> deviceRecords=deviceRecordPage.getContent();
        deviceRecords.stream().forEach(deviceRecord -> deviceRecord.setParams(JSON.toJSONString(deviceRecord.getParams())));
         page.setRecords(deviceRecordPage.getContent());
         page.setTotal(deviceRecordPage.getTotalElements());
         page.setCurrent(deviceRecordPage.getNumber());
        return new PageInfoBT<>(page);
    }

    /**
     * 新增设备统计表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(DeviceRecord deviceRecord) {
        deviceRecordService.insert(deviceRecord);
        return SUCCESS_TIP;
    }

    /**
     * 删除设备统计表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String deviceRecordId) {
        deviceRecordService.deleteById(deviceRecordId);
        return SUCCESS_TIP;
    }

    /**
     * 修改设备统计表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(DeviceRecord deviceRecord) {
        deviceRecordService.updateById(deviceRecord);
        return SUCCESS_TIP;
    }

    /**
     * 设备统计表详情
     */
    @RequestMapping(value = "/detail/{deviceRecordId}")
    @ResponseBody
    public Object detail(@PathVariable("deviceRecordId") String deviceRecordId) {
        return deviceRecordService.selectById(deviceRecordId);
    }
}
